package com.vcc.boss.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.NotFoundException;
import com.vcc.core.exception.ProgramException;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.UserRate;
import com.vcc.service.boss.request.rate.CreateUserRateDto;
import com.vcc.service.boss.request.rate.UpdateUserRateDto;
import com.vcc.service.boss.response.rate.UserRateVoBoss;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.BossUserService;
import com.vcc.service.service.RateService;
import com.vcc.service.service.UserRateService;
import com.vcc.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("bossRate")
public class RateController {

    @Autowired
    private RateService rateService;
    @Autowired
    private BossUserService bossUserService;
    @Autowired
    private UserRateService userRateService;
    @Autowired
    private UserService userService;


    @RestPack
    @SaCheckPermission(value = "client:user:rate:list:view")
    @SaCheckLogin
    @GetMapping("getUserRate")
    public UserRateVoBoss getUserRate(Long userId) {
        Long dealerId = userService.getDealerIdById(userId);
        if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())) {
            throw new ProgramException("无权查看该用户的参数配置");
        }
        try {
            UserRate userRate = userRateService.getByUserId(userId);
            return new UserRateVoBoss(userRate);
        } catch (NotFoundException e) {
            return new UserRateVoBoss();
        }
    }

    @RestPack
    @SaCheckPermission(value = "client:user:rate:list:edit")
    @SaCheckLogin
    @PostMapping("setUserRate")
    public Boolean setUserRate(@RequestBody @Validated CreateUserRateDto createUserRateDto) {
        Long dealerId = userService.getDealerIdById(createUserRateDto.getUserId());
        if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())) {
            throw new ProgramException("无权操作该用户的参数配置");
        }
        if (StpUtil.hasPermission("client:user:rate:activity:set")) {
            if (RegexUtil.checkObjectIsNotNull(createUserRateDto.getActivityRate())) {
                if (RegexUtil.checkObjectIsNull(createUserRateDto.getActivityRate()) || createUserRateDto.getActivityRate().compareTo(BigDecimal.ZERO) < 0) {
                    throw new ProgramException("活动汇率不能小于0");
                }
                if (RegexUtil.checkStringIsNull(createUserRateDto.getActivityStartDate())) {
                    throw new ProgramException("活动汇率必须选择活动开始时间");
                }
                if (RegexUtil.checkStringIsNull(createUserRateDto.getActivityEndDate())) {
                    throw new ProgramException("活动汇率必须选择活动结束时间");
                }
            }
        } else {
            createUserRateDto.setActivityRate(null);
            createUserRateDto.setActivityStartDate(null);
            createUserRateDto.setActivityEndDate(null);
        }

        if (!userService.existsById(createUserRateDto.getUserId())) {
            throw new ProgramException("没有当前用户");
        }
        userRateService.createUserRate(createUserRateDto);
        return true;
    }


    @RestPack
    @SaCheckPermission(value = "client:user:rate:list:batch:edit")
    @SaCheckLogin
    @PostMapping("batchSetUserRate")
    public Boolean batchSetUserRate(@RequestBody @Validated UpdateUserRateDto updateUserRateDto) {
        for (Long userId : updateUserRateDto.getUserId()) {
            Long dealerId = userService.getDealerIdById(userId);
            if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())) {
                throw new ProgramException("无权操作该用户的参数配置");
            }
        }
        if (RegexUtil.checkObjectIsNotNull(updateUserRateDto.getActivityRate())) {
            if (RegexUtil.checkObjectIsNull(updateUserRateDto.getActivityRate()) || updateUserRateDto.getActivityRate().compareTo(BigDecimal.ZERO) < 0) {
                throw new ProgramException("活动汇率不能小于0");
            }
            if (RegexUtil.checkStringIsNull(updateUserRateDto.getActivityStartDate())) {
                throw new ProgramException("活动汇率必须选择活动开始时间");
            }
            if (RegexUtil.checkStringIsNull(updateUserRateDto.getActivityEndDate())) {
                throw new ProgramException("活动汇率必须选择活动结束时间");
            }
        }
        userRateService.batchUpdateUserRate(updateUserRateDto);
        return true;
    }
}
